package edu.zueb.sport.rest;

import edu.zueb.sport.dao.UserMapper;
import edu.zueb.sport.entity.User;
import edu.zueb.sport.enums.UserStatusEnum;
import edu.zueb.sport.exception.RestException;
import edu.zueb.sport.rest.api.Page;
import edu.zueb.sport.rest.api.RestResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

/**
 * @author jasonfu
 * @date 2024/9/11 14:14
 */
@Tag(name = "用户模块", description = "用户模块")
@Slf4j
@RestController
@RequestMapping(UserRestController.PATH_USER)
public class UserRestController {

    public static final String PATH_USER = "/api/user";

    @Autowired
    UserMapper userMapper;

    @PostMapping(value = "/insert")
    public RestResponse<User> insert(@RequestBody User user) {
        try {
            userMapper.insertUser(user);
        } catch (DuplicateKeyException e) {
            throw new RestException("用户名已存在");
        }
        return RestResponse.success(user);
    }

    @PostMapping(value = "/list")
    public RestResponse<Page<User>> list(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "userType", required = false) String userType,
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        int totalCount = userMapper.countByPage(name, username, userType);
        if (totalCount == 0) {
            return RestResponse.success(Page.empty());
        }
        List<User> users = userMapper.listByPage(name, username, userType, pageNo, pageSize);
        return RestResponse.success(Page.asPage(totalCount, pageSize, users));
    }

    @PostMapping(value = "/update")
    public RestResponse<User> update(@RequestBody User user) {
        int rows = userMapper.updateUser(user);
        if (rows != 1) {
            throw new RestException("更新失败");
        }
        return RestResponse.success(user);
    }

    @PostMapping(value = "/delete")
    public RestResponse<Boolean> delete(@RequestBody User user) {
        int rows = userMapper.deleteUser(user.getUserId());
        if (rows != 1) {
            throw new RestException("删除失败");
        }
        return RestResponse.success(true);
    }

    @PostMapping("/enable")
    public RestResponse<Boolean> enable(@RequestBody User user) {
        user.setUserStatus(UserStatusEnum.ENABLED.getValue());
        int rows = userMapper.updateStatus(user);
        if (rows != 1) {
            throw new RestException("修改用户状态失败");
        }
        return RestResponse.success(true);
    }

    @PostMapping("/disable")
    public RestResponse<Boolean> disable(@RequestBody User user) {
        user.setUserStatus(UserStatusEnum.DISABLED.getValue());
        int rows = userMapper.updateStatus(user);
        if (rows != 1) {
            throw new RestException("修改用户状态失败");
        }
        return RestResponse.success(true);
    }
}
